package com.suwenhao.service;


import com.suwenhao.dao.MeetingDao;
import com.suwenhao.dao.MenuDao;
import com.suwenhao.entity.Meeting;
import com.suwenhao.entity.Menu;
import com.suwenhao.enums.MeetingStatusEnum;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @Author:suwenhao
 * @Description: 测试
 * @Date: Created in 2020/9/22 17:35
 */
public class MeetingService {

    private MeetingDao meetingDao = new MeetingDao();

    public List<Meeting> listPage() {
        return meetingDao.listPage();
    }

    public void addMeeting(Meeting meeting) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd  HH:mm:ss");

        meeting.setPublishDate(sdf.format(new Date()));

        //默认状态是未开始
        meeting.setStatus(MeetingStatusEnum.NOT_AT_THE.getValue());

        meeting.setMakeUser(Arrays.toString(meeting.getMakeUsers()));
        meetingDao.addMeeting(meeting);
    }


    //定时修改状态
    public void upStatusTimer() {

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        //查询所有数据
        List<Meeting> list = meetingDao.listPage();
        list.stream().forEach(n -> {
            String start = n.getStartTime();
            String end = n.getEndTime();
            try {
                //开始时间
                long startTime = sdf.parse(start).getTime();
                //结束时间
                long endTime = sdf.parse(end).getTime();
                //当前时间
                long currentTime = new Date().getTime();

                Meeting meeting = new Meeting();

                meeting.setId(n.getId());

                if (startTime <= endTime && endTime >= currentTime) {
                    //进行中
                    meeting.setStatus(MeetingStatusEnum.THE_ONGOING.getValue());

                    System.out.println("进行中修改了状态"+meeting.getId());
                    meetingDao.updateStatusById(meeting);


                }else{
                    //已结束
                    meeting.setStatus(MeetingStatusEnum.EXIT.getValue());

                    System.out.println("已结束修改了状态"+meeting.getId());
                    meetingDao.updateStatusById(meeting);
                    System.out.println(meeting.getId());

                }

            } catch (ParseException e) {
                e.printStackTrace();
            }
        });

    }

    public Meeting getMeetingById(Integer id) {
        return meetingDao.getMeetingById(id);
    }

    public List<Integer> listUserId(Integer id) {
        return meetingDao.listUserId(id);
    }

    public void attendTheMeeting(Integer u_id, Integer c_id) {
        meetingDao.attendTheMeeting(u_id,c_id);
    }


}
